//
//  AdvancedApproveBankCardVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/6.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "AdvancedApproveBankCardVC.h"
#import "HeaderBankCardView.h"
#import "ImportTextView.h"
#import "BankCardListVC.h"
#import "AATransitionVC.h"
#import <TZImagePickerController.h>
#import "MyRequestTools.h"

@interface AdvancedApproveBankCardVC ()<TZImagePickerControllerDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) HeaderBankCardView *headerCardView;

@property (nonatomic, strong) ImportTextView *nameView;
@property (nonatomic, strong) ImportTextView *identityView;
@property (nonatomic, strong) ImportTextView *cardNumberView;
@property (nonatomic, strong) ImportTextView *phoneView;

@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation AdvancedApproveBankCardVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    [self getBankInfo];
    // Do any additional setup after loading the view.
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
}

- (void)backAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)initUI {
    
    self.title = @"高级实名认证";
    self.mainScrollView.backgroundColor = ListMaoBackgroundColor;
    
    [self.headerCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(0.5f);
        make.height.mas_equalTo(165);
    }];
    
    [self.nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.headerCardView.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [self.identityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.nameView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.cardNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.identityView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.cardNumberView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
    UILabel *tinkLabel = [[UILabel alloc] init];
    tinkLabel.text = @"绑定的银行卡必须为本人所有";
    tinkLabel.textColor = HexColor(0x666666);
    tinkLabel.font = [UIFont systemFontOfSize:13];
    [self.mainScrollView addSubview:tinkLabel];
    [tinkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView.mas_left).offset(20);
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(15);
    }];
    
    UIButton *bankCardListBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bankCardListBtn setTitle:@"查看支持银行卡" forState:UIControlStateNormal];
    [bankCardListBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    bankCardListBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [bankCardListBtn addTarget:self action:@selector(bankCardListAction) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrollView addSubview:bankCardListBtn];
    [bankCardListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(tinkLabel);
        make.right.mas_equalTo(self.headerView.mas_right).offset(-16);
        make.size.mas_equalTo(CGSizeMake(100, 19));
    }];
}

- (void)getBankInfo {
    
    [MyRequestTools GetAdvancedApproveBankCardWithParameters:nil Success:^(id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        NSString *url = dataDic[@"bankCardImgUrl"];
        if (url.length) {
            self.headerCardView.imageUrl = url;
            self.headerCardView.cameraImageView.hidden = YES;
            self.headerCardView.removeBtn.hidden = NO;
            self.headerCardView.hintLabel.hidden = YES;
            [self.headerCardView.bankCardImageView sd_setImageWithURL:[NSURL URLWithString:url]];
        }
        self.cardNumberView.leftTextView.text = dataDic[@"bankCardnumber"];
        self.phoneView.leftTextView.text = dataDic[@"bankPhone"];
        self.identityView.leftTextView.text = dataDic[@"identityCardNumber"];
        self.nameView.leftTextView.text = dataDic[@"userName"];
        
        [self cheakData];
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
    }];
}


//弹窗
-(void)showPickerView {
    
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4/1.0];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 210, ScreenWidthMao, 210)];
    whiteView.backgroundColor = HexColor(0xE7E7ED);
    [backgroundView addSubview:whiteView];
    
    UIButton *firstBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    firstBtn.backgroundColor = [UIColor whiteColor];
    firstBtn.frame = CGRectMake(0, 0, ScreenWidthMao, 50);
    [whiteView addSubview:firstBtn];
    
    UIButton *secondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    secondBtn.backgroundColor = [UIColor whiteColor];
    secondBtn.frame = CGRectMake(0, 50.5f, ScreenWidthMao, 50);
    [secondBtn setTitle:@"拍摄/上传" forState:UIControlStateNormal];
    [secondBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    secondBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [secondBtn addTarget:self action:@selector(secondBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:secondBtn];
    
    UIButton *thirdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    thirdBtn.backgroundColor = [UIColor whiteColor];
    thirdBtn.frame = CGRectMake(0, 101, ScreenWidthMao, 50);
    [thirdBtn setTitle:@"从相册选择照片" forState:UIControlStateNormal];
    [thirdBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    thirdBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [thirdBtn addTarget:self action:@selector(thirdBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:thirdBtn];
    
    UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomBtn.backgroundColor = [UIColor whiteColor];
    bottomBtn.frame = CGRectMake(0, 210 - 50, ScreenWidthMao, 50);
    [bottomBtn setTitle:@"取消" forState:UIControlStateNormal];
    [bottomBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    bottomBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [bottomBtn addTarget:self action:@selector(cancleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:bottomBtn];
    
    [UIView animateWithDuration:0.3 animations:^{
        [[HFQCommonTools lastWindow] addSubview:backgroundView];
    }];
    
}

- (void)secondBtnAction:(UIButton *)sender {
    
    UIImagePickerController *imagePickerController =[[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.navigationBar.barTintColor = [UIColor whiteColor];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    [self cancleBtnAction:sender];
}

- (void)thirdBtnAction:(UIButton *)sender {
    
    TZImagePickerController *imgPickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imgPickerVc.allowPickingVideo = NO;
    imgPickerVc.allowPickingGif = NO;
    imgPickerVc.allowPickingMultipleVideo = NO;
    imgPickerVc.naviBgColor = ThemeMaoColor;
    [imgPickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> * photos, NSArray *assets, BOOL result) {
        for (UIImage *image in photos)
        {
            [self dealWithImg:image];
        }
    }];
    [self presentViewController:imgPickerVc animated:YES completion:nil];
    [self cancleBtnAction:sender];
}

- (void)cancleBtnAction:(UIButton *)sender {
    
    
    UIView *whiteView = sender.superview;
    UIView *backgroundView = whiteView.superview;
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        backgroundView.hidden = YES;
        [backgroundView removeFromSuperview];
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *icon = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(icon,nil,nil,nil);
    [self dealWithImg:icon];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealWithImg:(UIImage *)image {
    
    [ASJF_Service uploadPictureWithData:image Success:^(id  _Nullable responseObject) {
        
        NSString *url = responseObject[@"data"][@"filePath"];
        
        self.headerCardView.imageUrl = url;
        self.headerCardView.cameraImageView.hidden = YES;
        self.headerCardView.removeBtn.hidden = NO;
        self.headerCardView.hintLabel.hidden = YES;
        [self.headerCardView.bankCardImageView sd_setImageWithURL:[NSURL URLWithString:url]];
        
        [self cheakData];
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
    }];
}

- (void)cheakData {
    
    self.nextBtn.alpha = 0.5f;
    self.nextBtn.userInteractionEnabled = NO;
    if (!self.headerCardView.imageUrl.length) {
        return;
    }else if (!self.cardNumberView.leftTextView.text.length) {
        return;
    }else if (![HFQCommonTools isValidateMobile:self.phoneView.leftTextView.text]) {
        return;
    }
    self.nextBtn.alpha = 1;
    self.nextBtn.userInteractionEnabled = YES;
}

- (void)bankCardListAction {
    
    BankCardListVC *vc = [[BankCardListVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tiJiaoAction {
    
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
    
    [mutDic setObject:self.headerCardView.imageUrl forKey:@"bankCardImgUrl"];
    [mutDic setObject:self.cardNumberView.leftTextView.text forKey:@"bankCardnumber"];
    [mutDic setObject:self.phoneView.leftTextView.text forKey:@"bankPhone"];
    
    [HFQIndicationView showInView:self.view];
    [MyRequestTools AdvancedApproveBankCardWithParameters:mutDic Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        
        AATransitionVC *vc = [[AATransitionVC alloc] init];
        vc.showType = AATransitionShowTypeSucceed;
        [self.navigationController pushViewController:vc animated:YES];
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        
    }];
    
}

//检测手机号、银行卡
- (void)textFieldChanged:(UITextField *)textField {
    
    [self cheakData];
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao, 600);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}


- (UIView *)headerView {
    
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 55)];
        _headerView.backgroundColor = [UIColor whiteColor];
        [self.mainScrollView addSubview:_headerView];
        
        UIImageView *lineImageView = [[UIImageView alloc] init];
        lineImageView.backgroundColor = HexColor(0xBA915E);
        [_headerView addSubview:lineImageView];
        [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(57, 0.5f));
            make.centerX.centerY.mas_equalTo(_headerView);
        }];
        
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = @"身份信息";
        leftLabel.textColor = HexColor(0x383838);
        leftLabel.font = [UIFont systemFontOfSize:13];
        [_headerView addSubview:leftLabel];
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_headerView);
            make.right.mas_equalTo(lineImageView.mas_left).offset(-8);
        }];
        
        UILabel *leftLogoLabel = [[UILabel alloc] init];
        leftLogoLabel.text = @"1";
        leftLogoLabel.textColor = [UIColor whiteColor];
        leftLogoLabel.backgroundColor = ThemeMaoColor;
        leftLogoLabel.layer.cornerRadius = 7.5;
        leftLogoLabel.layer.masksToBounds = YES;
        leftLogoLabel.font = [UIFont systemFontOfSize:13];
        [leftLogoLabel setTextAlignment:NSTextAlignmentCenter];
        [_headerView addSubview:leftLogoLabel];
        
        [leftLogoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_headerView);
            make.right.mas_equalTo(leftLabel.mas_left).offset(-8);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        
        UILabel *rightLogoLabel = [[UILabel alloc] init];
        rightLogoLabel.text = @"2";
        rightLogoLabel.textColor = [UIColor whiteColor];
        rightLogoLabel.backgroundColor = ThemeMaoColor;
        rightLogoLabel.layer.cornerRadius = 7.5;
        rightLogoLabel.layer.masksToBounds = YES;
        rightLogoLabel.font = [UIFont systemFontOfSize:13];
        [rightLogoLabel setTextAlignment:NSTextAlignmentCenter];
        [_headerView addSubview:rightLogoLabel];
        [rightLogoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_headerView);
            make.left.mas_equalTo(lineImageView.mas_right).offset(8);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.text = @"银行卡信息";
        rightLabel.textColor = HexColor(0x383838);
        rightLabel.font = [UIFont systemFontOfSize:13];
        [_headerView addSubview:rightLabel];
        [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_headerView);
            make.left.mas_equalTo(rightLogoLabel.mas_right).offset(8);
        }];
        
    }
    return _headerView;
}

- (UIButton *)nextBtn {
    
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.alpha = 0.5f;
        _nextBtn.userInteractionEnabled = NO;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)HexColor(0xD6C096).CGColor, (__bridge id)HexColor(0xBEA471).CGColor];
        gradientLayer.locations = @[@0, @1];
        gradientLayer.startPoint = CGPointMake(0, 1);
        gradientLayer.endPoint = CGPointMake(1, 1);
        gradientLayer.frame = CGRectMake(0, 0, ScreenWidthMao, 50);
        gradientLayer.cornerRadius = 4;
        gradientLayer.masksToBounds = YES;
        [_nextBtn.layer addSublayer:gradientLayer];
        [_nextBtn addTarget:self action:@selector(tiJiaoAction) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor whiteColor];
        label.text = @"提交";
        [_nextBtn addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(_nextBtn);
        }];
        
        [self.view addSubview:_nextBtn];
    }
    return _nextBtn;
}

- (HeaderBankCardView *)headerCardView {
    
    if (!_headerCardView) {
        _headerCardView = [[HeaderBankCardView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPickerView)];
        [_headerCardView addGestureRecognizer:tap];
        
        [self.mainScrollView addSubview:_headerCardView];
    }
    return _headerCardView;
}

- (ImportTextView *)nameView {
    
    if (!_nameView) {
        _nameView = [[ImportTextView alloc] init];
        _nameView.leftTextView.placeholder = @"持卡人名";
        _nameView.titleLabel.text = @"持卡人名";
        _nameView.leftTextView.userInteractionEnabled = NO;
        
        [self.mainScrollView addSubview:_nameView];
    }
    return _nameView;
}
- (ImportTextView *)identityView {
    
    if (!_identityView) {
        _identityView = [[ImportTextView alloc] init];
        _identityView.leftTextView.placeholder = @"18位身份证号";
        _identityView.titleLabel.text = @"身份证号";
        _identityView.leftTextView.userInteractionEnabled = NO;
        
        [self.mainScrollView addSubview:_identityView];
    }
    return _identityView;
}
- (ImportTextView *)cardNumberView {
    
    if (!_cardNumberView) {
        _cardNumberView = [[ImportTextView alloc] init];
        _cardNumberView.leftTextView.placeholder = @"银行卡号";
        _cardNumberView.titleLabel.text = @"银行卡号";
        [_cardNumberView.leftTextView setKeyboardType:UIKeyboardTypeNumberPad];
        [_cardNumberView.leftTextView addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mainScrollView addSubview:_cardNumberView];
    }
    return _cardNumberView;
}
- (ImportTextView *)phoneView {
    
    if (!_phoneView) {
        _phoneView = [[ImportTextView alloc] init];
        _phoneView.leftTextView.placeholder = @"请输入银行预留手机号";
        _phoneView.titleLabel.text = @"预留手机";
        [_phoneView.leftTextView addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
        [_phoneView.leftTextView setKeyboardType:UIKeyboardTypeNumberPad];
        _phoneView.lineImageView.hidden = YES;
        
        [self.mainScrollView addSubview:_phoneView];
    }
    return _phoneView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
 
 - (void)bankInfoWithPhone {
 
 SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
 con.scNaigationDelegate = self;
 con.iCardType = TIDBANK;
 con.isDisPlayTxt = YES;
 [self presentViewController:con animated:YES completion:NULL];
 }
 // 银行卡识别接口
 - (void)sendBankCardInfo:(NSString *)bank_num BANK_NAME:(NSString *)bank_name BANK_ORGCODE:(NSString *)bank_orgcode BANK_CLASS:(NSString *)bank_class CARD_NAME:(NSString *)card_name {
 NSLog(@"bank_num = %@, bank_name = %@, bank_orgcode = %@, bank_class = %@, card_name = %@",bank_num,bank_name,bank_orgcode,bank_class,card_name);
 //    self.cardNumberView.writeTextField.text = bank_num;
 //    self.bankView.writeTextField.text = bank_name;
 }
 - (void)sendBankCardImage:(UIImage *)BankCardImage {
 NSLog(@"%@",BankCardImage);
 //    [self dealWithImg:BankCardImage];
 }

*/

@end
